<template>
  <div class="mod-user">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.userName" placeholder="用户名或学院" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('sys:user:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('sys:user:delete')" type="danger" @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>

    <div style="margin: 0 0 5px 0;">
      <el-radio-group v-model="identity" @change="identityHandle" fill="#00b7c3">
        <!--0专家 1管理员  -->
        <el-radio-button :label="0">评委</el-radio-button>
        <el-radio-button :label="1">管理员</el-radio-button>
      </el-radio-group>
    </div>

    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <!-- <el-table-column prop="userId" header-align="center" align="center" width="80" label="ID">
      </el-table-column> -->
      <el-table-column prop="username" header-align="center" align="center" label="用户名">
      </el-table-column>
      <el-table-column prop="roleName" header-align="center" align="center" label="角色">
        <template slot-scope="scope">
          <div v-for="(k, i) in scope.row.roleName" :key="k">{{ k }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="name" header-align="center" align="center" label="姓名">
      </el-table-column>
      <el-table-column prop="college" header-align="center" align="center" label="学院">
      </el-table-column>
      <el-table-column prop="expertStatu" header-align="center" align="center" label="是否赋予校级权限" width="150">
        <template slot-scope="scope">
          <div v-if="scope.row.expertStatu == null">该用户不是专家</div>
          <el-switch v-else active-color="#13ce66" inactive-color="#ff4949" v-model="scope.row.expertStatu"
            :active-value="2" :inactive-value="1" @change="giveAuth(scope.row.expertStatu, scope.row.userId)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="danger">禁用</el-tag>
          <el-tag v-else size="small">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" width="180" label="创建时间">
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('sys:user:update')" type="text" size="small"
            @click="addOrUpdateHandle(scope.row.userId)">修改</el-button>
          <el-button v-if="isAuth('sys:user:delete')" type="text" size="small"
            @click="deleteHandle(scope.row.userId, scope.row.name)">
            删除</el-button>
          <el-button type="text" size="small" @click="lookOver(scope.row.userId)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[1, 10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗查看 -->
    <el-dialog :title="'提示'" :visible.sync="isLookOver" width="40%">
      <div>
        <el-form :model="lookOverForm" label-width="80px">
          <el-form-item label="姓名:" prop="name">
            <div>{{ lookOverForm.name }}</div>
          </el-form-item>
          <el-form-item label="身份:">
            <div v-if="lookOverForm.expertStatu == null">
              <div v-if="lookOverForm.roleIdList == 2">
                <el-tag type="success">院级管理员</el-tag>
              </div>
              <div v-else-if="lookOverForm.roleIdList == 4">
                <el-tag type="success">校级管理员</el-tag>
              </div>
            </div>
            <div v-else>
              <div v-if="lookOverForm.expertStatu == 1">
                <el-tag>院级专家</el-tag>
              </div>
              <div v-else-if="lookOverForm.expertStatu == 2">
                <el-tag>校级专家</el-tag>
              </div>
            </div>
          </el-form-item>
          <el-form-item label="登录邮箱:">
            <div>{{ lookOverForm.username }}</div>
          </el-form-item>
          <el-form-item label="手机号:">
            <div>
              {{ lookOverForm.mobile }}
            </div>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="isLookOver = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './user-add-or-update'
export default {
  data() {
    return {
      identity: 0,
      dataForm: {
        userName: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      isLookOver: false,
      lookOverForm: {},
    }
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList()
  },
  methods: {
    //身份转换
    identityHandle() {
      this.pageIndex = 1
      console.log('身份转换', this.identity);
      this.getDataList()
    },
    //赋予专家校级权限
    giveAuth(expertStatu, userId) {
      this.$http({
        url: this.$http.adornUrl('/sys/user/updateExpert'),
        method: 'post',
        data: this.$http.adornData({
          userId,
          expertStatu
        })
      }).then(({
        data
      }) => {
        if (data.code == 0) {
          this.getDataList()
          this.$message({
            type: 'success',
            message: '修改成功'
          })
        } else {
          if (data.code == 400) {
            this.$message.error(data.msg)
          } else if (data.code == 500) {
            this.$message.error('错误：' + data.msg)
          }
        }
      })
      this.getDataList()
    },
    //查看管理员/专家信息
    lookOver(id) {
      console.log('id', id);
      this.isLookOver = true
      this.$http({
        url: this.$http.adornUrl(`/sys/user/info/${id}`),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({
        data
      }) => {
        if (data.code == 0) {
          this.lookOverForm = data.user
          console.log('data', data);
          console.log('><>>>', this.lookOverForm);
        } else {
          this.$message.error('错误:' + data.code + data.msg)
        }
      })
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/sys/user/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'username': this.dataForm.userName,
          'identity': this.identity
        })
      }).then(({
        data
      }) => {
        console.log('ident', this.identity.toString(), this.identity);
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      // console.log('isChange',isChange);
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id, name) {
      var userIds = id ? [id] : this.dataListSelections.map(item => {
        return item.userId
      })
      var names = name ? [name] : this.dataListSelections.map(item => {
        return item.name
      })
      // console.log(names);
      // `确定对名为=${userIds.join(',')}进行${id ? '删除' : '批量删除'}操作?`
      this.$confirm(`确定对姓名为:<strong>${names.join(',')}</strong> 进行${names ? '删除' : '批量删除'}操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        dangerouslyUseHTMLString: true
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/user/delete'),
          method: 'post',
          data: this.$http.adornData(userIds, false)
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => { })
    }
  }
}
</script>
